﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;
using Bzure.Common.Core;
using System.ComponentModel.Composition;

namespace Bzure.Mvc.ResourceAccess
{
    [Export(typeof(IController)), ExportMetadata("Name", "Resource"), ExportMetadata("Area", "ResourceAccess")]
    //[PartCreationPolicy(CreationPolicy.NonShared)]
    public class ResourceController : Controller
    {
        [OutputCache(Duration = 600, Location = OutputCacheLocation.ServerAndClient)]
        public ActionResult Script(string location, string id)
        {
            Response.Cache.SetOmitVaryStar(true);
            IContractFactory<IResourceAccess> builder = Actor.Public.GetNamedExport<IContractFactory<IResourceAccess>>(location);
            if (builder.IsNull())
                return Content("no resource");
            return Content(builder.Build().GetScript(id), "text/javascript");
        }
        [OutputCache(Duration = 600, Location = OutputCacheLocation.ServerAndClient)]
        public ActionResult CSS(string location, string id)
        {
            Response.Cache.SetOmitVaryStar(true);
            IContractFactory<IResourceAccess> builder = Actor.Public.GetNamedExport<IContractFactory<IResourceAccess>>(location);
            if (builder.IsNull())
                return Content("no resource");
            return Content(builder.Build().GetCSS(id), "text/css");
        }
        [OutputCache(Duration = 600, Location = OutputCacheLocation.ServerAndClient)]
        public ActionResult Image(string location, string id)
        {
            Response.Cache.SetOmitVaryStar(true);
            IContractFactory<IResourceAccess> builder = Actor.Public.GetNamedExport<IContractFactory<IResourceAccess>>(location);
            if (builder.IsNull())
                return Content("no resource");
            return File(builder.Build().GetImage(id), "image/png");
        }
    }
}
